package service

import (
	"fmt"
	"github.com/smartwalle/alipay/v3"
	"log"
	"net/http"
)

var client *alipay.Client

func InitAlipayClient() {

	var err error
	client, err = alipay.New("2021005114691079", "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC8ol9RAQSO/Ee9eSXqU1iaos57YHMd951UEOYychwvSdQaQbTF1I6S5zvu5Jy1jksMlnb8sbLkQjC+afns3ln9EyQZLaVa6s+K2bZ7Y/s7AboeJmBP2opBuOL5Su9KWzMcfB5BsVrueASvpkxX6RhS/ohivnLOFmhEJVCjeCsb2r4BSAOXU4u954C+lVHrAw84mpw8CiLHHrnaZcT/gyOILAg8feN50qXPqoTF+lXWtnM5VNpvynoUfVoXZU9ECLk0X0JqKjfOUDSODwV8C0GFDSUkIfm/y3q6orZoWpJoBw9izUfBCIKOnjP0EBpm302fGjce/Ii0NoInnqU72HYLAgMBAAECggEADnmTQN8IH4TCaIq0Wx/cK7mUiKklDEak/v9w+qwA96v9p6olGg0uh51Q3wsR72ik36loJgnoxVVC8kmlkQSa+S2P1pLHHovoMypzs1I/xA5k3s1pFJebhmGOoTyb0iPfby8jDz1x0dzJ10S2lQRhBnVbt+APZTsY8ym9311oSXHErQyU3GLkB6jcFztuXrQE/cy6boXDFjYLMHK9EdevfWUiiglc3T3/mS/k/4soV6l2ZATFGf/QSzdHDHPcVUA8/nRgoHfgVw2CyT9uy4+cDimyHjZsKrZGhUN+eYF6GPGaA7I5jafwd+X7b6RQZTve1ZQpJuraWHHcnqVBiPMtQQKBgQD3spxknY0zA+WwcmENl67JEzacIRj6TNnIrfyItt8zbop11NGTSz+kOWgVK2L1ZNDWgLb78GzVwZhImrNYCdtRyoU+x3Ov58tLA1s5cQXcQ3ed0V2AyhOD81RJxx62A3BKL3hmaPZv6EYTZPht1nhM3f35gGJYn/kIyHx4QIYIKwKBgQDC9PaOsBPNbFUBNmPvGqcCgLpfvhT2y8LcLPmGKvmyCwpjXDl6JcZTiL6iVWdhHajUSN9gny+B3BrcdUSIC6wX6ngRvns8kKaEghaZ2e2kfdEst68fxJKwmraJskoVYoRlVOqhG7kv05NdAIVfNjyLf3GFFflDtpCZKUhYV5N5oQKBgQCOfrwlzdVbEdTjSwm2ThqJJnp2GzxJUki/gAgVhk6lQeR5oPLn75QvH69W7lzBmzvhCq86BLg3yGWY7xZZgWIgjYdV9FwDSC/VeZrTUsfyTQXVToqh+p57fFJzFw/92cCRjPmQwLV074TQx3d7iLQwVE01abwqC+2yNdQVQdlZfQKBgHTZPa3n+vZ0p75CS0NURiVJ4mrgEtCY8AHnMucneuErnN5QuGJtoaEPpLLHXV7n1/jkdH4E6wR6V98qqhigZlUvQyffcx2p1SQ2lDVCWRzuDCExNtVjjEgtcpq1MZH/qjrR2o8VfrkbpodneIjIiSFTEUcjFHNxSNmD1MieNNYBAoGAN205UpLBTZkm+fEx2Y6KikBH5aHcuVSQj/TsD+7qrZIfAMPmQPpBFGqpAx+dtG52THNyWUlVJ9N62cHV1HL65uNd2PTO3k5Rz4j5ib9XwFZtl2O4ESSy9oQN/RlseygSxJ3OSx8Pc4RIKlExjfjyripwx126M2ig/OsCt2K9u94=", false)
	if err != nil {
		log.Fatalf("初始化支付宝客户端失败: %v", err)
	}

	err = client.LoadAliPayPublicKey("MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxTEZi2diYjc2HCiZrfMyVADInY5B00YT1dEen+XrGrd42+he9yfXG1qGANh5EzvJu2+3rjRpIU8tOmRTvvIbOHWQ/qH9vqaGsODCzwgZdSXI7e3gtRp0OTNeq6ehRG35mvy2WwsA8LDBtUk1Sl9qTgzuFuvnTKzQgKIrwbVJ/tmCNp/Ia/K7+nALB+YEypK44uxeetVOMPttzkPZSR0ZOrOHZgWhD73zfLnLG3fTnmrU+EhPM5X91yOQHQc4B3M2ZOS1qyA9PdwvJhA5ESnBqp7wd1BW9facyXQn1kC39d4AnkzZBJ7kr2gIuhEVcHu9au7hF4hx4W4CcWN7CeOGJQIDAQAB")
	if err != nil {
		log.Fatalf("加载支付宝公钥失败: %v", err)
	}

	// 设置异步通知验签

}
func CreateAppPayment(outTradeNo, subject, totalAmount string) (payParam string, err error) {

	client, err = alipay.New("2021005114691079", "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC8ol9RAQSO/Ee9eSXqU1iaos57YHMd951UEOYychwvSdQaQbTF1I6S5zvu5Jy1jksMlnb8sbLkQjC+afns3ln9EyQZLaVa6s+K2bZ7Y/s7AboeJmBP2opBuOL5Su9KWzMcfB5BsVrueASvpkxX6RhS/ohivnLOFmhEJVCjeCsb2r4BSAOXU4u954C+lVHrAw84mpw8CiLHHrnaZcT/gyOILAg8feN50qXPqoTF+lXWtnM5VNpvynoUfVoXZU9ECLk0X0JqKjfOUDSODwV8C0GFDSUkIfm/y3q6orZoWpJoBw9izUfBCIKOnjP0EBpm302fGjce/Ii0NoInnqU72HYLAgMBAAECggEADnmTQN8IH4TCaIq0Wx/cK7mUiKklDEak/v9w+qwA96v9p6olGg0uh51Q3wsR72ik36loJgnoxVVC8kmlkQSa+S2P1pLHHovoMypzs1I/xA5k3s1pFJebhmGOoTyb0iPfby8jDz1x0dzJ10S2lQRhBnVbt+APZTsY8ym9311oSXHErQyU3GLkB6jcFztuXrQE/cy6boXDFjYLMHK9EdevfWUiiglc3T3/mS/k/4soV6l2ZATFGf/QSzdHDHPcVUA8/nRgoHfgVw2CyT9uy4+cDimyHjZsKrZGhUN+eYF6GPGaA7I5jafwd+X7b6RQZTve1ZQpJuraWHHcnqVBiPMtQQKBgQD3spxknY0zA+WwcmENl67JEzacIRj6TNnIrfyItt8zbop11NGTSz+kOWgVK2L1ZNDWgLb78GzVwZhImrNYCdtRyoU+x3Ov58tLA1s5cQXcQ3ed0V2AyhOD81RJxx62A3BKL3hmaPZv6EYTZPht1nhM3f35gGJYn/kIyHx4QIYIKwKBgQDC9PaOsBPNbFUBNmPvGqcCgLpfvhT2y8LcLPmGKvmyCwpjXDl6JcZTiL6iVWdhHajUSN9gny+B3BrcdUSIC6wX6ngRvns8kKaEghaZ2e2kfdEst68fxJKwmraJskoVYoRlVOqhG7kv05NdAIVfNjyLf3GFFflDtpCZKUhYV5N5oQKBgQCOfrwlzdVbEdTjSwm2ThqJJnp2GzxJUki/gAgVhk6lQeR5oPLn75QvH69W7lzBmzvhCq86BLg3yGWY7xZZgWIgjYdV9FwDSC/VeZrTUsfyTQXVToqh+p57fFJzFw/92cCRjPmQwLV074TQx3d7iLQwVE01abwqC+2yNdQVQdlZfQKBgHTZPa3n+vZ0p75CS0NURiVJ4mrgEtCY8AHnMucneuErnN5QuGJtoaEPpLLHXV7n1/jkdH4E6wR6V98qqhigZlUvQyffcx2p1SQ2lDVCWRzuDCExNtVjjEgtcpq1MZH/qjrR2o8VfrkbpodneIjIiSFTEUcjFHNxSNmD1MieNNYBAoGAN205UpLBTZkm+fEx2Y6KikBH5aHcuVSQj/TsD+7qrZIfAMPmQPpBFGqpAx+dtG52THNyWUlVJ9N62cHV1HL65uNd2PTO3k5Rz4j5ib9XwFZtl2O4ESSy9oQN/RlseygSxJ3OSx8Pc4RIKlExjfjyripwx126M2ig/OsCt2K9u94=", false)
	if err != nil {
		log.Fatalf("初始化支付宝客户端失败: %v", err)
	}

	err = client.LoadAliPayPublicKey("MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxTEZi2diYjc2HCiZrfMyVADInY5B00YT1dEen+XrGrd42+he9yfXG1qGANh5EzvJu2+3rjRpIU8tOmRTvvIbOHWQ/qH9vqaGsODCzwgZdSXI7e3gtRp0OTNeq6ehRG35mvy2WwsA8LDBtUk1Sl9qTgzuFuvnTKzQgKIrwbVJ/tmCNp/Ia/K7+nALB+YEypK44uxeetVOMPttzkPZSR0ZOrOHZgWhD73zfLnLG3fTnmrU+EhPM5X91yOQHQc4B3M2ZOS1qyA9PdwvJhA5ESnBqp7wd1BW9facyXQn1kC39d4AnkzZBJ7kr2gIuhEVcHu9au7hF4hx4W4CcWN7CeOGJQIDAQAB")
	if err != nil {
		log.Fatalf("加载支付宝公钥失败: %v", err)
	}
	var p = alipay.TradeAppPay{}
	p.NotifyURL = "https://yourdomain.com/alipay/notify" // 异步通知地址
	p.Body = subject
	p.Subject = subject
	p.OutTradeNo = outTradeNo
	//p.TotalAmount = 0.1
	p.ProductCode = "QUICK_MSECURITY_PAY" // APP支付固定值

	param, err := client.TradeAppPay(p)
	if err != nil {
		return "", fmt.Errorf("创建APP支付失败: %v", err)
	}

	return param, nil
}
func HandlePaymentNotify(w http.ResponseWriter, r *http.Request) {
	// 解析表单数据
	if err := r.ParseForm(); err != nil {
		http.Error(w, "Bad Request", http.StatusBadRequest)
		return
	}

	// 获取通知
	noti, err := client.GetTradeNotification(r)
	if err != nil {
		log.Printf("验证通知失败: %v", err)
		http.Error(w, "Verify Failed", http.StatusBadRequest)
		return
	}

	// 处理业务逻辑
	switch noti.TradeStatus {
	case "TRADE_SUCCESS":
		// 支付成功处理
		log.Printf("订单 %s 支付成功", noti.OutTradeNo)
		// TODO: 更新订单状态等业务逻辑
	case "TRADE_FINISHED":
		// 交易结束处理
		log.Printf("订单 %s 交易完成", noti.OutTradeNo)
	default:
		log.Printf("订单 %s 状态: %s", noti.OutTradeNo, noti.TradeStatus)
	}

	// 必须返回success给支付宝
	w.Write([]byte("success"))
}
